/*
 * Powered By [leyvi]
 * Since 2014 - 2016
 */
var qrcodeModel = require('../model/Qrcode');
var db = require('../../utils/db/mysqlDb');
var common = require('../../utils/commonUtils');
var Promise = require('promise');
var qrcodeDb = {

};

var defaultSelectColumn = ' Id,ios_code,and_code,ios_url,and_url';

var defaultTable = 't_qrcode';
//getById
qrcodeDb.getById = function(id){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable+ ' where 1=1 ';
        if(typeof id === "object"){
            sql+='and Id= ' + db.escape(id.id);
        }else{
            sql+=' and Id= ' + db.escape(id);
        }
        db.query(sql).then(function(rows){
            if(rows[0]){
                fulfill(qrcodeModel.dbConvertModel(rows[0]));
            }else{
                fulfill(null);
            }             
        }, function(err){
            reject(err);
        });
    });
};

//save
qrcodeDb.save = function(obj, connection){
    return db.save('insert into ' + defaultTable+ '  set ?',
             qrcodeModel.modelConvertDb(obj), connection);
};

//update
qrcodeDb.update = function(obj, connection){
    var sql = 'update ' + defaultTable+ '  set ';
    if(typeof obj.iosCode != "undefined"){
        sql+='ios_code=:iosCode,';
    }
    if(typeof obj.andCode != "undefined"){
        sql+='and_code=:andCode,';
    }
    if(typeof obj.iosUrl != "undefined"){
        sql+='ios_url=:iosUrl,';
    }
    if(typeof obj.andUrl != "undefined"){
        sql+='and_url=:andUrl,';
    }
    sql+='where ';
        sql+='Id=:id ';
        sql = sql.replace(',where', '  where');
    return db.update(sql, obj, connection);
};

//delete
qrcodeDb.delete = function(id, connection){
    var sql = 'delete from ' + defaultTable+ '  where 1=1 ';
    if(typeof id === "object"){
        sql+=' and Id= ' + db.escape(id.id);
    }else{
        sql+=' and Id= ' + db.escape(id);
    }
    return db.update(sql, {}, connection);
};

//查询条件
var selectParams = function(params){
    var sql = '';
    if(params && typeof params.id != "undefined"){
        sql+=' and  Id=' + db.escape(params.id);
    }
    if(params && typeof params.iosCode != "undefined"){
        sql+=' and  ios_code=' + db.escape(params.iosCode);
    }
    if(params && typeof params.andCode != "undefined"){
        sql+=' and  and_code=' + db.escape(params.andCode);
    }
    if(params && typeof params.iosUrl != "undefined"){
        sql+=' and  ios_url=' + db.escape(params.iosUrl);
    }
    if(params && typeof params.andUrl != "undefined"){
        sql+=' and  and_url=' + db.escape(params.andUrl);
    }
    return sql;
};


//count
qrcodeDb.count = function(params){
    return new Promise(function(fulfill, reject){
        var sql = 'select count(*) as total from ' + defaultTable;
        sql += ' where 1=1 ';
        sql += selectParams(params);
        db.query(sql).then(function(rows){
            fulfill(rows[0].total);
        }, function(err){
            reject(err);
        });
    });
};

//page
qrcodeDb.getByPage = function(params, pageNo, pageSize, orderby){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
        sql+=' where 1=1 ';
        sql += selectParams(params);
        sql+=' order by ' + orderby;
        if(pageNo <= 0){
            pageNo = 1;
        }
        pageNo = common.isNumber(pageNo) ? pageNo : 1;
        pageSize = common.isNumber(pageSize) ? pageSize : 10;
        sql += ' limit ';
        sql += (pageNo - 1) * pageSize;
        sql +=',';
        sql +=pageSize;
       
        db.query(sql).then(function(rows){
            var array = new Array();
            for(var p in rows){
                if(rows[p])array.push(qrcodeModel.dbConvertModel(rows[p]));
            }
            fulfill(array);
        }, function(err){
            reject(err);
        });
    });
};

qrcodeDb.getByParams = function(params, orderby){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
        sql+=' where 1=1 ';
        sql += selectParams(params);
        sql+=' order by ' + orderby;
        db.query(sql).then(function(rows){
            var array = new Array();
            for(var p in rows){
                if(rows[p])array.push(qrcodeModel.dbConvertModel(rows[p]));
            }
            fulfill(array);
        }, function(err){
            reject(err);
        });
    });
};

qrcodeDb.db = db;
module.exports = qrcodeDb;